package com.example.taximeter.controller;

import com.example.taximeter.bean.*;
import com.example.taximeter.mapper.CompanyMapper;
import com.example.taximeter.service.CompanyService;
import com.example.taximeter.service.FactService;
import com.example.taximeter.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 出租车公司
 */
@RestController
@RequestMapping(value = "/company")
public class CompanyController {

    private CompanyService companyService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);


    @Autowired
    public CompanyController(CompanyService companyService,UserService userService){
        this.companyService = companyService;
        this.userService = userService;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Company> findCompanyList(){
        return companyService.findCompanyList();
    }

    //查看出租车公司列表
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public ReturnInfo findCompanyAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                       @RequestBody Company company) {
        //添加日志
        userService.addUserLog(7,company.toString());
        return companyService.findCompanyALL(Integer.valueOf(pageNum), Integer.valueOf(pageSize), company);
    }

    //新增出租车公司
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ReturnInfo addCompany(@RequestBody  @Validated(GroupCreate.class) Company company){
        String mes = companyService.addCompany(company);
        if(mes.equals("成功")){
//            logger.info("新增出租车公司"+company.toString());
            //添加日志
            userService.addUserLog(4,company.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //修改出租车公司
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ReturnInfo updateCompany(@RequestBody  @Validated(GroupUpdate.class) Company company){
        String mes = companyService.updateCompany(company);
        if(mes.equals("成功")){
//            logger.info("修改出租车公司"+company.toString());
            //添加日志
            userService.addUserLog(5,company.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //删除出租车公司
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public ReturnInfo deleteCompany(@PathVariable Integer id){
        String mes = companyService.deleteCompany(id);
        if(mes.equals("成功")){
//            logger.info("删除出租车公司"+String.valueOf(id));
            //添加日志
            userService.addUserLog(6,String.valueOf(id));
        }
        return ReturnInfo.info1(mes);
    }
}
